package batch.udb;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.*;

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.util.Calendar;

public class makeLoginHistoryFileForGbrCRM
{
    private static String sTmpDate = "";

    public makeLoginHistoryFileForGbrCRM() {
        ;
    }

    public static void main(String [] args) {
        Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info("( * ) Start.");

        if(args.length == 1){
            sTmpDate = args[0];
        }

        makeLoginHistoryFileForGbrCRM mkUser = new makeLoginHistoryFileForGbrCRM();
        mkUser.startMake();

        Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info("( * ) End.");
    }

    //Make a log file and a data file.
    public void startMake(){
        Connection   conn = null;
        Statement    stmt = null;
        ResultSet    rs   = null;
        OutputStreamWriter oswFile = null;

        Calendar calen = Calendar.getInstance();

        String TODAY     = "";
        String YESTERDAY = "";

        Date ye1day = null;

        if(sTmpDate.equals("") || sTmpDate == null){
            calen.add(Calendar.DATE, -1);
            ye1day = calen.getTime();

            TODAY = DateFormatUtils.format(new Date(), "yyyyMMdd");
            YESTERDAY = DateFormatUtils.format(ye1day, "yyyyMMdd");
        }
        else{
            TODAY = sTmpDate;

            java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat("yyyyMMdd");
            java.util.Date dTmpDate = sformat.parse(TODAY, new java.text.ParsePosition(0));

            calen.setTime(dTmpDate);
            calen.add(Calendar.DATE, -1);
            ye1day = calen.getTime();

            YESTERDAY = DateFormatUtils.format(ye1day, "yyyyMMdd");
        }

        String fileNm  = "/home01/sfc/batch/udb/data/lgn_"  + TODAY;

        int cnt = 0;
        int rsCnt = 0;

        StringBuffer sbTemp = new StringBuffer();

        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT A.USER_NO                                         \n")
                             .append("     , B.CREATE_YMDHMS                                   \n")
                             .append("     , B.HIS_CD                                          \n")
                             .append("  FROM G_MB_MEMBER          A                            \n")
                             .append("     , G_MB_LOGIN_HISTORY   B                            \n")
                             .append(" WHERE A.USER_NO = B.USER_NO                             \n")
                             .append("   AND A.REG_COUNTRY = 'gbr'                             \n")
                             .append("   AND A.DELETED IS NULL                                 \n")
                             .append("   AND B.CREATE_YMDHMS BETWEEN '" + YESTERDAY + "000000' \n")
                             .append("                           AND '" + YESTERDAY + "235959' \n");

                             //.append("   AND B.CREATE_YMDHMS < '" + TODAY + "000000'           \n");

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@eu-anydb:1521:ORAEU", "gsms", "any02gs");
            conn.setAutoCommit(true);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sQuery.toString());
			rsCnt = rs.getMetaData().getColumnCount();

            oswFile = new OutputStreamWriter(new FileOutputStream(fileNm, false), "ISO-8859-1");

            while(rs.next()){
				sbTemp.setLength(0);

                for(int i=1;i<=rsCnt;i++) {
                    sbTemp.append(ObjectUtils.toString(rs.getObject(i), ""));
                    //user_no|create_ymdhms|his_cd
                    if( i == rsCnt ){
                        sbTemp.append("\n");
                    }
                    else{
                        sbTemp.append("|");
                    }
                }

                oswFile.write(sbTemp.toString());
                cnt++;

                if(cnt % 500 == 0){
                    Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info("here we are == " + cnt);
                }
            }

            Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info("Total Count : " + cnt);
        }
        catch(Exception ex) {
            ex.printStackTrace();

            Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info(ex.toString());
            StackTraceElement[] stElement = ex.getStackTrace();
            for(int p=0; p<stElement.length;p++){
                Logger.getLogger("udb.makeLoginHistoryFileForGbrCRM").info(stElement[p].toString());
            }
        }
        finally {
            if (rs   != null) try { rs.close(); }   catch (Exception e) { e.printStackTrace(); }
            if (stmt != null) try { stmt.close(); } catch (Exception e) { e.printStackTrace(); }
            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            if (oswFile != null) try { oswFile.close(); } catch (Exception e) { e.printStackTrace(); }
        }
    }
}
